package yiminsu.com.cn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import yiminsu.com.cn.beans.HotelsketchVO;
import yiminsu.com.cn.beans.LeisurehotelUserHotel;
import yiminsu.com.cn.beans.LeisurehotelUserMerchant;
import yiminsu.com.cn.beans.UserMinshuListVo;
import yiminsu.com.cn.mapper.LeisurehotelUserMapper;
import yiminsu.com.cn.service.LeisurehotelUserService;
import yiminsu.com.cn.utils.ResultsUtil;
import yiminsu.com.cn.utils.UUIDUtils;
import yiminsu.com.cn.utils.UserListUtils;
import yiminsu.com.cn.utils.UserTextUtils;

@Service
public class LeisurehotelUserServiceImpl implements LeisurehotelUserService {

	@Autowired
	private LeisurehotelUserMapper mapper;

	private ResultsUtil resultsUtil = new ResultsUtil();

	/**
	 * 新增子账号
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public ResultsUtil insertUser(String userId, String userName, String phone, String msid) {
		
		try {
			Map<String, String> map = new HashMap<String, String>();
			if ((phone == null && userName == null)) {
				resultsUtil.setMsg("查询不能为空");
				resultsUtil.setType(false);
			}

			map.put("phone", "86-" + phone);

			//判断用户表里有没有这一条信息
			String zzhuserid = mapper.getUserCount(map);
			if (zzhuserid == null) {
				resultsUtil.setMsg("该手机号尚未注册逸民宿");
				resultsUtil.setType(false);
				resultsUtil.setData(zzhuserid);
				return resultsUtil;
			}

			map.remove("phone");
			map.put("userName", userName);
			zzhuserid = mapper.getUserCount(map);

			if (zzhuserid == null) {
				resultsUtil.setMsg("该昵称与手机号昵称不对应");
				resultsUtil.setType(false);
				resultsUtil.setData(zzhuserid);
				return resultsUtil;
			}
			LeisurehotelUserMerchant leisurehotelUserMerchant = new LeisurehotelUserMerchant();
			leisurehotelUserMerchant.setFid(userId);
			leisurehotelUserMerchant.setZid(zzhuserid);
			leisurehotelUserMerchant.setId(UUIDUtils.UUIDTo32());

			int leisurehotel_user_merchant = mapper.insertLeisurehotelUserMerchant(leisurehotelUserMerchant);

			LeisurehotelUserHotel leisurehotelUserHotel = new LeisurehotelUserHotel();
			leisurehotelUserHotel.setId(UUIDUtils.UUIDTo32());
			leisurehotelUserHotel.setUserid(zzhuserid);
			String[] msids = msid.split(",");
			for (int i = 0; i < msids.length; i++) {
				leisurehotelUserHotel.setHid(Integer.parseInt(msids[i]));
				int inserttype = mapper.insertLeisurehotelUserHotel(leisurehotelUserHotel);
				if (inserttype == 0) {
					throw new Exception();
				}
			}
			if (leisurehotel_user_merchant != 0) {
				map.put("userId", zzhuserid);
				int updatetype = mapper.updateUserType(map);
				if (updatetype == 0) {
					throw new Exception();
				}
				resultsUtil.setMsg("新增成功");
				resultsUtil.setType(true);
			} else {
				resultsUtil.setMsg("新增失败");
			}
		} catch (Exception e) {			
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			resultsUtil.setMsg("系统异常，新增失败。");
		}
		return resultsUtil;
	}

	@Override
	public ResultsUtil getUserHotelsketch(String userId) {
		
		List<HotelsketchVO> list = mapper.getUserHotelsketch(userId);
		resultsUtil.setData(list);
		resultsUtil.setMsg("查询成功");
		resultsUtil.setType(true);
		return resultsUtil;
	}

	@Override
	public ResultsUtil getUser(String userId) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<UserListUtils> userListutils = new ArrayList<UserListUtils>();
		List<UserTextUtils> userList = mapper.queryUserFid(userId);
		
		for(int i=0;i<userList.size();i++) {
			UserListUtils user = new UserListUtils();
			user.setRegisterPhoneNum(userList.get(i).getRegisterPhoneNum());
			user.setUserId(userList.get(i).getUserId());
			user.setUserName(userList.get(i).getUserName());
			List<UserMinshuListVo> list = mapper.getUserMinshuList(userList.get(i).getUserId());
			user.setList(list);
			userListutils.add(user);
		}
		resultsUtil.setData(userListutils);
		resultsUtil.setMsg("查询成功");
		resultsUtil.setType(true);
		return resultsUtil;
	}

	@Override
	public ResultsUtil getUser(String phone, String userName) {
		// TODO Auto-generated method stub
		return null;
	}

}
